package br.com.ianalise.modelo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import br.com.ianalise.negocio.excecoes.IAnaliseException;
import br.com.ianalise.negocio.pojo.ConfigRede;

public class ConexaoBDConfigRede {

	private Connection conn;

	public ConexaoBDConfigRede() throws IAnaliseException {
		this.conn = ConexaoBD.getConnection();
		try {
			this.conn.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new IAnaliseException(e.toString());
		}
	}

	public boolean alterarConfigRede(ConfigRede configRede) throws IAnaliseException {

		try {
			PreparedStatement stmt = conn
					.prepareStatement("UPDATE config_rede SET tipoNo = ?, ipServidor = ?, porta = ? WHERE idConfig=?");
			stmt.setString(1, configRede.getTipoNo());
			stmt.setString(2, configRede.getIpServidor());
			stmt.setString(3, configRede.getPorta());
			stmt.setInt(4, 1);

			stmt.execute();
			stmt.close();
			conn.close();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new IAnaliseException(e.toString());
		}

	}
	
	public ConfigRede consultarConfigRede(int idConfigRede) throws IAnaliseException {
		ConfigRede configRede = null;

        ResultSet rs = null;
        try {
                PreparedStatement stmt = conn
                                .prepareStatement("SELECT * FROM config_rede WHERE idConfig = "
                                                + idConfigRede);
                rs = stmt.executeQuery();

                while (rs.next()) {
                	configRede = new ConfigRede();
                	configRede.setTipoNo(rs.getString("tipoNo"));
                	configRede.setIpServidor(rs.getString("ipServidor"));
                	configRede.setPorta(rs.getString("porta"));
                }

                stmt.close();
                conn.close(); //fecha de fato a conexão

        } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new IAnaliseException(e.toString());
        }
        return configRede;

}
}